<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="查询关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入名称或描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="mgr10"
        ></el-date-picker>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row type="flex">
      <el-col>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['category:doc:list']"
          >新增分类</el-button
        >
      </el-col>
      <el-col class="tr">
        <right-toolbar
          :show-search.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-col>
    </el-row>

    <el-table class="mgt10" v-loading="loading" :data="menuList">
      <el-table-column
        prop="name"
        label="分类名称"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="desc"
        width="160"
        label="分类描述"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['category:doc:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['category:doc:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      background
      :current-page="queryParams.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="queryParams.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="380px"
      append-to-body
      @close="reset"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input placeholder="请输入分类名称" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="分类描述" prop="desc">
          <el-input
            type="textarea"
            v-model="form.desc"
            placeholder="请输入分类描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAllCategory,
  addCategory,
  detailCategory,
  editCategory,
  delCategory
} from '@/api/category'
import RightToolbar from '@/components/RightToolbar'
export default {
  name: 'Menu',
  components: { RightToolbar },
  data() {
    return {
      total: 0,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        keyword: undefined,
        dateRange: [],
        pagenum: 1,
        pagesize: 5
      },
      // // 表单参数
      form: {
        name: '', // 分类名称
        desc: '' // 分类描述
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        desc: [{ required: true, message: '请输入分类描述', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询菜单列表 */
    async getList() {
      this.loading = true
      const { _RejCode, data } = await getAllCategory(this.queryParams).catch(
        (err) => err
      )
      if (_RejCode !== '00000000') {
        this.total = 0
        this.menuList = []
        this.loading = false
        return
      }
      this.menuList = data.list
      this.total = data.count
      this.loading = false
    },
    // 一页显示多少条数据
    handleSizeChange(pageSize) {
      this.queryParams.pagesize = pageSize
      this.queryParams.pagenum = 1
      this.getList()
    },
    // 当前页数，当前在第几页
    handleCurrentChange(curPage) {
      this.queryParams.pagenum = curPage
      this.getList()
    },
    // 修改
    async handleUpdate(row) {
      const { _RejCode, data } = await detailCategory(row._id)
      if (_RejCode !== '00000000') {
        this.loading = false
        return
      }
      this.form = data
      this.open = true
      this.title = '编辑分类'
    },
    // // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    async reset() {
      await this.$nextTick()
      // 表单参数
      this.form = {}
      this.$refs['form'].resetFields()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs['queryForm'].resetFields()
      this.handleQuery()
    },
    // /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加分类'
    },
    // /** 提交按钮 */
    submitForm: function () {
      console.log(this.form)
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '添加分类') {
            addCategory(this.form)
              .then((response) => {
                // this.msgSuccess('新增成功')
                this.$message.success(response._RejMsg)
                this.open = false
                this.getList()
              })
              .catch((err) => {
                console.log(err)
              })
          } else {
            editCategory(this.form)
              .then((response) => {
                // this.msgSuccess('新增成功')
                this.$message.success(response._RejMsg)
                this.open = false
                this.getList()
              })
              .catch((err) => {
                console.log(err)
              })
          }
        }
      })
    },
    // /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的分类?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return delCategory({ id: row._id })
        })
        .then((res) => {
          this.getList()
          this.$message.success(res._RejMsg)
        })
    }
  }
}
</script>
